package com.example.zhongxiangmo;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class XiangMuFragment extends Fragment {


    private View view;
    private RecyclerView mRlv;
    private ArrayList<BannerBean.DataBean> dataBeans;
    private ArrayList<ArticleBean.DataBean.DatasBean> list;
    private String bannerUrl = "https://www.wanandroid.com/banner/json";
    private String articleUrl = "https://www.wanandroid.com/article/list/0/json";
    private XiangMuAdapter xiangMuAdapter;
    private XiangMuAdapter xiangMuAdapter1;

    public XiangMuFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_xiangmu, container, false);
        initView(inflate);
        initListener();
        return inflate;
    }

    private void initListener() {
        xiangMuAdapter.setOnItemClickListener(new XiangMuAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int i) {
                Intent intent = new Intent(getActivity(), BaiDuActivity.class);
                String link = list.get(i).getLink();
                intent.putExtra("link",link);
                startActivity(intent);
            }
        });

        xiangMuAdapter.setOnItemLongClickListener(new XiangMuAdapter.OnItemLongClickListener() {
            @Override
            public void OnItemLongClick(int i) {

            }
        });



    }

    private void initView(View inflate) {
        mRlv = (RecyclerView) inflate.findViewById(R.id.rlv);

        dataBeans = new ArrayList<>();
        list = new ArrayList<>();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRlv.setLayoutManager(linearLayoutManager);

        xiangMuAdapter = new XiangMuAdapter(getActivity(), dataBeans, list);


        mRlv.setAdapter(this.xiangMuAdapter);
        initbanner();
        initlist();
        registerForContextMenu(mRlv);
    }

    private void initlist() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(articleUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    int code = connection.getResponseCode();
                    if (code == HttpURLConnection.HTTP_OK){
                        InputStream is = connection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String len = null;
                        StringBuffer sbf = new StringBuffer();
                        while ((len = br.readLine()) != null){
                            sbf.append(len);
                        }
                        is.close();
                        br.close();
                        String json = sbf.toString();
                        Gson gson = new Gson();

                        ArticleBean articleBean = gson.fromJson(json, ArticleBean.class);
                        ArticleBean.DataBean data = articleBean.getData();
                        final List<ArticleBean.DataBean.DatasBean> datas = data.getDatas();

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                list.addAll(datas);
                                xiangMuAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initbanner() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(bannerUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    int code = connection.getResponseCode();
                    if (code == HttpURLConnection.HTTP_OK){
                        InputStream is = connection.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String len = null;
                        StringBuffer sbf = new StringBuffer();
                        while ((len = br.readLine()) != null){
                            sbf.append(len);
                        }
                        is.close();
                        br.close();
                        String json = sbf.toString();
                        Gson gson = new Gson();
                        BannerBean bannerBean = gson.fromJson(json, BannerBean.class);
                        final List<BannerBean.DataBean> data = bannerBean.getData();

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dataBeans.addAll(data);
                                xiangMuAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0,0,0,"标为未读");
        menu.add(0,1,0,"聊天置顶");
        menu.add(0,2,0,"删除该聊天");


        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 0:
                Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }


        return super.onContextItemSelected(item);
    }
}
